#ifndef _CTERMBOX_H_
#define _CTERMBOX_H_

#include <buola/gui/cwindow.h>
#include <buola/utility/ccursorpos.h>
#include <buola/utility/usignal.h>

namespace buola {

class CVirtualTerm;
    
class CTermBox : public gui::CWindow
{
public:
    CTermBox();
    ~CTermBox();

private:
    void CreateVT();

public:
    void OpenApp(const std::string &pCommand);
    void OpenShell();

    std::wstring GetTitle();
    bool IsExited();

    void SelectionToClipboard(gui::CClipboard *pCB);

    void DrawLine(img::CGC *pGC,double pY,int pLine,int pFrom,int pTo);
    void DrawLines(img::CGC *pGC,int pFrom,int pTo);

    CCursorPos GetPosition(const CPoint_d &pPoint);

private:
    void CalcCellSize();
    void ChangeFontSize(double pChange);
    
    virtual void OnExpose(const gui::CExposeEvent &pE);

    virtual void OnChar(const gui::CKeyEvent &pE);
    virtual void OnKeyDown(const gui::CKeyEvent &pE);

    virtual void OnButtonDown(const gui::CMouseEvent &pE);
    virtual void OnButtonUp(const gui::CMouseEvent &pE);
    virtual void OnMouseMove(const gui::CMouseEvent &pE);
    virtual void OnMouseWheel(const gui::CMouseEvent &pE);

    virtual void OnSize(const gui::CGeometryEvent &pE);

    virtual void OnFocusIn(const gui::CFocusEvent &pE);
    virtual void OnFocusOut(const gui::CFocusEvent &pE);

    void OnVTSize();
    void OnVTDirty(int pFrom,int pTo);
    void OnScrollBar(int pValue);

public:
    USignal<void()> sTitleChanged;
    USignal<void()> sExited;

private:
    std::unique_ptr<CVirtualTerm> mVT;
    gui::PScrollBar mScrollBar;

    img::PFont mFont;
    img::PFont mBoldFont;
    double mFontSize;
    CSize_d mCellSize;

    struct
    {
        bool mSelecting;
        bool mOn;
        bool mSquare;
        CCursorPos mLast;
        CCursorPos mFrom;
        CCursorPos mTo;
    } mSelect;

    int mFirstLine;

    bool mFocused;
};

typedef intrusive_ptr<CTermBox> PTermBox;

/*namespace buola*/ }

#endif
